﻿using B.S.DataBase.MyProject.Domain.患者移动端;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Read.API.Appcation.Commands.PatientMovement;

namespace MedicalPCTerminal.Read.API.Appcation.CommandHandlers.PatientMovement
{
    /// <summary>
    /// 处理登陆命令
    /// </summary>
    public class LoginCommandHandler : IRequestHandler<LoginCommand, APIResult<List<Me>>>
    {
        private readonly SqlSugarContext sqlSugar;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="meRepository">仓储</param>
        public LoginCommandHandler(SqlSugarContext sqlSugar)
        {
            this.sqlSugar = sqlSugar;
        }
        public async Task<APIResult<List<Me>>> Handle(LoginCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<List<Me>> result = new APIResult<List<Me>>();
                var entity = sqlSugar.db.Queryable<Me>().Where(x => x.UserName == request.UserName && x.Password == request.Password).ToList();
                if (entity != null)
                {
                    result.Code = APIEnum.登录成功;
                    result.Msg = APIEnum.登录成功.ToString();
                    result.data = entity;
                }
                else
                {
                    result.Code = APIEnum.登录失败密码或用户名错误;
                    result.Msg = APIEnum.登录失败密码或用户名错误.ToString();
                }
                return await Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }

        }
    }
}
